package vn.gq.cd.model.bean;

import org.json.JSONException;
import org.json.JSONObject;

import vn.gq.app4vn.doctruyen.Configuration;
import android.content.Context;
import android.content.SharedPreferences;

public class AdmobItem {
	
	public static final String TYPE_BANNER = "banner";
	public static final String TYPE_FULLSCREEN = "fullscreen";
	public static final String TYPE_OBANNER = "o_banner";
	public static final String TYPE_OINTERSITIAL = "o_intersitial";
	public static final String KEY_LOADED = "loaded";
	
	
	private String banner = Configuration.Ad.ADMOB_DEFAULT_ID;
	private String fullscreen = Configuration.Ad.ADMOB_DEFAULT_ID;
	private String o_banner = Configuration.Ad.NONADMOB_ID;
	private String o_intersitial = Configuration.Ad.NONADMOB_INTERSITIAL_ID;
	private boolean loaded = false;
	
	public AdmobItem() {
		
	}
	
	public AdmobItem(String banner, String fullscreen, String o_banner, String o_intersitial) {
		setAdTypeBanner(banner);
		setAdTypeIntersitial(fullscreen);
		setAdTypeOBanner(o_banner);
		setAdTypeOIntersitial(o_intersitial);
	}
	
	public AdmobItem(JSONObject input) {
		try {
			setAdTypeBanner(input.getString(TYPE_BANNER));
			setAdTypeIntersitial(input.getString(TYPE_FULLSCREEN));
			setAdTypeOBanner(input.getString(TYPE_OBANNER));
			setAdTypeOIntersitial(input.getString(TYPE_OINTERSITIAL));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public void setAdTypeBanner(String _banner) {
		banner = _banner;
	}
	
	public void setAdTypeIntersitial(String _fullscreen) {
		fullscreen = _fullscreen;
	}
	
	public String getAdTypeBanner() {
		return banner;
	}
	
	public String getAdTypeIntersitial() {
		return fullscreen;
	}
	
	public JSONObject toJSON() {
		JSONObject saveObj = new JSONObject();
		try {
			saveObj.put(TYPE_BANNER, banner);
			saveObj.put(TYPE_FULLSCREEN, fullscreen);
			saveObj.put(TYPE_OBANNER, o_banner);
			saveObj.put(TYPE_OINTERSITIAL, o_intersitial);
			saveObj.put(KEY_LOADED, loaded);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return saveObj;
	}
	
	public void setPreference(Context context) {
		SharedPreferences pref = context.getSharedPreferences("pref_ads", Context.MODE_PRIVATE);
		pref.edit().putString("adUnitId", toJSON().toString()).commit();
	}
	
	public static AdmobItem load(Context context) {
		SharedPreferences pref = context.getSharedPreferences("pref_ads", Context.MODE_PRIVATE);
		String admobItemStr = pref.getString("adUnitId", new AdmobItem().toJSON().toString());
		if (admobItemStr != null) {
			try {
				AdmobItem item = new AdmobItem(new JSONObject(admobItemStr));
				return item;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return new AdmobItem();
	}

	public boolean isLoaded() {
		return loaded;
	}

	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}

	public String getAdTypeOBanner() {
		return o_banner;
	}

	public void setAdTypeOBanner(String o_banner) {
		this.o_banner = o_banner;
	}

	public String getAdTypeOIntersitial() {
		return o_intersitial;
	}

	public void setAdTypeOIntersitial(String o_intersitial) {
		this.o_intersitial = o_intersitial;
	}
}
